﻿using System;
using System.Threading;
using System.Windows.Forms;
using TimeTracker.data;
using TimeTracker.excep;

namespace TimeTracker.app
{
    internal class Program
    {
        private static Program _instance = new Program();
        private static AbstractConnector _abstractConnector;

        public static AbstractConnector Connector
        {
            get
            {
                if (!_abstractConnector.LoggedIn()) throw new NotConnectedException();
                return _abstractConnector;
            }
        }


        public static Program GetInstance()
        {
            return _instance ?? (_instance = new Program());
        }

        public static void ShowGui()
        {
            new Thread(new ThreadStart(ShowMainForm)).Start();
        }

        [STAThread]
        private static void ShowMainForm()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        public bool Connect(string server, string user, string password)
        {
            if (_abstractConnector != null &&_abstractConnector.LoggedIn()) throw new AlreadyConnectedException();
            _abstractConnector = RedminePhpConnector.GetInstance();
            return _abstractConnector.Login(server, user, password);
        }
    }
}